package com.liehacker.android.ontop;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.liehacker.android.util.Config;
import com.liehacker.android.util.DebugLog;


/**
 * 
 * 최상위 클래스
 * 공통 함수 & 변수
 * 
 * @author liehacker
 * 
 */
public class LiehackerSuperActivity extends Activity {
	
	public static Context context;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initContextVariables();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initContextVariables();
	}
	
	protected void initContextVariables() {
		LiehackerSuperActivity.context = this;
		Config.setContext(this);
	}
	
	
	protected void hideKeyboard(View editText) {
		if (editText != null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
		}
	}
	protected void showKeyboard(View editText) {
		if (editText != null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.toggleSoftInputFromWindow(editText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED,
							InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}	

		
	protected void showToast(String string) {
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
	}
	
	public void openChrome(String url) {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		context.startActivity(i);
	}
	
	protected boolean isPortrait() {
		int orientation = getRequestedOrientation();
		int orien = getWindowManager().getDefaultDisplay().getOrientation();
		DebugLog.d("full", "orien = " + orien);
		DebugLog.d("full", "orientation = " + orientation + " ActivityInfo.SCREEN_ORIENTATION_PORTRAIT = "
						+ ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		if (orientation != 0 && orientation != 1) {
			WindowManager mWMgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
			int width = mWMgr.getDefaultDisplay().getWidth();
			int height = mWMgr.getDefaultDisplay().getHeight();
			boolean result = width < height;
			DebugLog.d("full", "width = " + width + " height = " + height + ". is orientation portrait ? " + result);
			return result;
		} else {
			return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == orientation;
		}
	}
	
	protected long getResolutionProduction() {
		WindowManager mWMgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		int width = mWMgr.getDefaultDisplay().getWidth();
		int height = mWMgr.getDefaultDisplay().getHeight();
		return width * height;
	}
	
	protected void alertDialogShow(Context context, String strTitle, String strMsg, String strBtn) {
		new AlertDialog.Builder(context)
        .setTitle(strTitle)
        .setMessage(strMsg)
        .setNeutralButton(strBtn, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {                                        
                //...할일
            }
        })        
        .show();
	}
	
	protected void alertDialogShow(Context context, String strTitle, String strMsg, String strFirBtn, String strSecBtn) {
		new AlertDialog.Builder(context)
		.setTitle(strTitle)
		.setMessage(strMsg)
		.setPositiveButton(strFirBtn, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int whichButton) {                                        
		        //...할일
		    }
		})
		.setNegativeButton(strSecBtn, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int whichButton) {
		        //...할일
		    }
		})
		.show();
	}
	
}
